#![allow(dead_code)]
use crate::domain::CountNum;
use crate::domain::StatusBean;
use crate::domain::StatusQueryRequest;
use crate::serviceimpl::init_db;
use crate::utils::string_util::is_digit;
use crate::utils::string_util::right_find_under_line;
use sqlx::Error;
use substring::Substring;

pub async fn activate_all_statuses(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let statuses = &app_state.await.context.statuses;
	let vecter_iterator = ids.iter();
	for status_id in vecter_iterator {
		let _ = statuses.activate_status(*status_id).await;
	}
	Ok(0)
}

pub async fn activate_status(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.activate_status(id).await
}

pub async fn add_status(status:StatusBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.add_status(status).await
}

pub async fn clone_all_statuses(ids:Vec<i64>) ->Result<u64, Error> {
	let vecter_iterator = ids.iter();
	for status_id in vecter_iterator {
		let _ = clone_status(*status_id).await;
	}
	Ok(0)
}

pub async fn clone_status(id:i64) ->Result<u64, Error> {
	let mut status = find_status_by_id(id).await.unwrap();
	let name = find_status_indexed_name(status.name).await.unwrap();
	status.name = name;
	add_status(status).await
}

pub async fn count_search_statuses_by_fields_records(status_query_request:StatusQueryRequest) ->Result<CountNum, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.count_search_statuses_by_fields_records(status_query_request).await
}

pub async fn delete_all_statuses(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let statuses = &app_state.await.context.statuses;
	let vecter_iterator = ids.iter();
	for status_id in vecter_iterator {
		let _ = statuses.delete_status(*status_id).await;
	}
	Ok(0)
}

pub async fn delete_status(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.delete_status(id).await
}

pub async fn find_status_by_id(id:i64) ->Result<StatusBean, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.find_status_by_id(id).await
}

pub async fn find_status_by_name(name:String) ->Result<StatusBean, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.find_status_by_name(name).await
}

pub async fn list_active_statuses() ->Result<Vec<StatusBean>, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.list_active_statuses().await
}

pub async fn list_all_statuses() ->Result<Vec<StatusBean>, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.list_all_statuses().await
}

pub async fn search_statuses_by_fields(status_query_request:StatusQueryRequest) ->Result<Vec<StatusBean>, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.search_statuses_by_fields(status_query_request).await
}

pub async fn search_statuses_by_fields_by_page(status_query_request:StatusQueryRequest) ->Result<Vec<StatusBean>, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.search_statuses_by_fields_by_page(status_query_request).await
}

pub async fn soft_delete_all_statuses(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let statuses = &app_state.await.context.statuses;
	let vecter_iterator = ids.iter();
	for status_id in vecter_iterator {
		let _ = statuses.soft_delete_status(*status_id).await;
	}
	Ok(0)
}

pub async fn soft_delete_status(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.soft_delete_status(id).await
}

pub async fn toggle_one_status(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	let statuses = &app_state.await.context.statuses;
	let status = statuses.find_status_by_id(id).await.unwrap();
	if status.active == false {
		let _ = statuses.toggle_status(id).await;
	} else {
		let count = statuses.count_active_status_records().await.unwrap();
		if count.count_num > 1 {
			let _ = statuses.toggle_status(id).await;
		}
	}
	Ok(0)
}

pub async fn toggle_status(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.toggle_status(id).await
}

pub async fn update_status(status:StatusBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.statuses.update_status(status).await
}

pub async fn find_status_indexed_name(name:String) ->Result<String, Error> {
	let mut short_name = name.clone();
	if name.contains("_") && is_digit(name.substring(right_find_under_line(name.clone())+1,name.chars().count()).to_string()) {
		short_name = name.substring(0,right_find_under_line(name.clone())).to_string();
	}

	let app_state = init_db();
	let statuses = app_state.await.context.statuses.find_status_indexed_name(short_name.clone()).await.unwrap();

	let mut index_num = 1;
	for status in statuses {
		let name2 = status.name;
		if name2.contains("_") && is_digit(name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).to_string()) {
			let index = name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).parse::<i32>().unwrap();
			if index >= index_num {
				index_num = index + 1;
			}
		}
	}

	let index_name = short_name.clone() + "_" + &index_num.to_string();
	Ok(index_name)
}

